const {
    Router
} = require("express")
const Collect = require("../model/collect")
const Hotel = require("../model/Hotel")
const router = new Router()

// 收藏的所有数据请求
router.get("/collectList", async (req, res) => {
    const userId = req.userInfo._id
    // const list = await Hotel.find({}).limit(10*num);
    // 查询所有的该用户点赞信息
    const list = await Collect.find({ userId }).populate(["userId", "hotelId"]);
    res.status(200).json({ list });
})


// 收藏所有数据
router.get("/collectList1", async (req, res) => {
    const userId = req.userInfo._id
    // const list = await Hotel.find({}).limit(10*num);
    // 查询所有的该用户点赞信息
    const list = await Collect.find({ userId })
    res.status(200).json({ list });
})



// 判断是否收藏了
router.get("/cxcollect", async (req, res) => {
    const userId = req.userInfo._id
    const {
        hotelId,
    } = req.query;
    const list = await Collect.findOne({ userId, hotelId });
    if (list) {
        res.status(200).json(true);
return
    }
    res.status(200).json(false);

})



// 添加点赞的数据部分
router.post("/addcollect", async (req, res) => {
    // console.log(req.userInfo);
    const userId = req.userInfo._id
    // console.log(userId);
    let formData
    let upnumobj = {};
    const {
        hotelId,
    } = req.body;
    let addhouobj = {
        userId,
    }
        addhouobj.hotelId = hotelId
        upnumobj._id = hotelId
        formData = Hotel;
    

    const data=await Collect.create(addhouobj);
    await formData.findOneAndUpdate(upnumobj, { $inc: { collectNum: 1 } });
    res.status(200).json({
        status: 200,
        message: "收藏成功",
        data
    });
})

// 删除点赞的数据
// '/api/likey/deletecollect'
router.delete('/deletecollect', async (req, res) => {
    const userId = req.userInfo._id
    let formData;
    let dowmnumobj = {};
    let {
        hotelId,
    } = req.query;

    let delhouobj = {
        userId,
    }
        delhouobj.hotelId = hotelId
        formData = Hotel;
        dowmnumobj._id = hotelId

    // console.log(delhouobj);
    const data= await Collect.findOneAndDelete(delhouobj);

    await formData.findOneAndUpdate(dowmnumobj, { $inc: { collectNum: -1 } });
    res.status(200).json({
        status: 200,
        message: "取消收藏",
        data
    });
})






module.exports = router